package com.kanche.bigbaby.collection.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.kanche.bigbaby.R;
import com.kanche.bigbaby.collection.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//import com.kkche.bss.R;
//import com.kkche.bss.utils.StringUtils;

/**
 * Created by zhangsl on 14/12/19.
 */
public class ItemRadioGroupConfig extends ItemConfig implements RadioGroup.OnCheckedChangeListener{
    private int layoutId = R.layout.three_radiobutton_item;
    private List<Map<String, String>> radioGroupData = new ArrayList<Map<String, String>>();
    public ItemRadioGroupConfig(String key, String value, String label, List<Map<String, String>> radioGroupData) {
        super(key,value,label);
        this.radioGroupData = radioGroupData;
    }

    @Override
    public View getView(View convertView, Context mContext) {
        Holder holder = null;
        String canonicalName = ItemRadioGroupConfig.class.getCanonicalName();
        holder = new Holder();

        LayoutInflater inflater = LayoutInflater.from(mContext);

        convertView = inflater.inflate(layoutId, null);
        holder.radioGroup = (RadioGroup) convertView.findViewById(R.id.radiogroup);
        holder.personal = (RadioButton) convertView.findViewById(R.id.personal);
        holder.business = (RadioButton) convertView.findViewById(R.id.business);
        holder.lease = (RadioButton) convertView.findViewById(R.id.lease);
        if(radioGroupData.size() > 2){
            holder.lease.setVisibility(View.VISIBLE);
            holder.lease.setText(radioGroupData.get(2).get("label"));
        }
        holder.radioGroup.setOnCheckedChangeListener(this);
        holder.label = (TextView) convertView.findViewById(R.id.label);
        holder.type = canonicalName;
        holder.label.setText(getLabel());
        holder.personal.setText(radioGroupData.get(0).get("label"));
        holder.business.setText(radioGroupData.get(1).get("label"));
        String value = getValue();
        if (StringUtils.hasText(value)) {
            if (radioGroupData != null) {
                for (int i = 0; i < radioGroupData.size(); i++) {
                    if (value.equals(radioGroupData.get(i).get("key"))) {
                        int tag = 0;
                        if(i == 0)
                            tag = R.id.personal;
                        else if(i == 1)
                            tag = R.id.business;
                        else
                            tag = R.id.lease;
                        holder.radioGroup.check(tag);
                        break;
                    }
                }
            }
        } else {
        }
        if (holder.caption != null)
            holder.caption.setText(getCaption());
        return convertView;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int tag = 0;
        switch(checkedId){
            case R.id.personal:
                tag = 0;
                break;
            case R.id.business:
                tag = 1;
                break;
            case R.id.lease:
                tag = 2;
                break;
        }
        Map<String, String> kv = radioGroupData.get(tag);
        if (kv != null) setValue(kv.get("key"));
    }

    class Holder {
        String type;
        TextView caption, label;
        RadioGroup radioGroup;
        RadioButton personal,business,lease;
    }
}
